package com.example.springbootdemo.bean.response;

import com.alibaba.fastjson.JSONObject;
import com.example.springbootdemo.exception.BusinessResponseEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 响应信息
 * @author hh
 * @date 2022/1/19
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseInfo implements Serializable {

    private static final long serialVersionUID = 773672346926096198L;

    /**
     * 状态码
     */
    private int code;

    /**
     * 响应信息
     */
    private String msg;

    /**
     * 返回数据
     */
    private Object data;

    public static ResponseInfo success() {
        return new ResponseInfo(BusinessResponseEnum.SUCCESS.code(),
                BusinessResponseEnum.SUCCESS.value(),
                new JSONObject());
    }

    public static ResponseInfo success(Object data) {
        return new ResponseInfo(BusinessResponseEnum.SUCCESS.code(),
                BusinessResponseEnum.SUCCESS.value(),
                data);
    }

    public static ResponseInfo fail(){
        return new ResponseInfo(BusinessResponseEnum.COMMON_FAIL.code(),
                BusinessResponseEnum.COMMON_FAIL.value(),
                new JSONObject());
    }

    public static ResponseInfo fail(String msg){
        return new ResponseInfo(BusinessResponseEnum.COMMON_FAIL.code(),
                msg,
                new JSONObject());
    }

    public static ResponseInfo fail(int code, String msg){
        return new ResponseInfo(code,
                msg,
                new JSONObject());
    }

    public static ResponseInfo fail(BusinessResponseEnum exceptionEnum){
        return new ResponseInfo(exceptionEnum.code(),
                exceptionEnum.value(),
                new JSONObject());
    }

}
